Each problem must be submitted as its own .cpp file. Grading for each problem is as follows:
Characters
- Write a complete C++ program that does the following:
- It asks the user to enter an odd integer n that is between 7 and 19, inclusive.
- It repeatedly reads n from the user until the supplied value of n is legal.
- Passes integer n to a function called printTriangle that does the following:
- It prints out a triangular picture with a border (as shown in the sample run, but with width and height given by n). Starting with the first row inside the border, the first row of the triangle consists of upper case A, the second row consists of upper case B, and so on.
Sample run of program:
Enter an odd integer between 7 and 19: 7
*******
* A*
* BB*
* CCC*
* DDDD*
*EEEEE*
*******
- Write a complete C++ program that does the following:
- Declares a 3 x 3 char array and passes it to a function called fillCharArray that does the following:
- Fills a 3 x 3 char array with random lower case letters.
- Look at the ASCII table
- Lower case letters are ASCII 97 to 122 while upper case letters are ASCII 65 to 90.
- You can generate a random number in this range and cast it as a char to obtain a random letter.
- E.g., (char) (rand() % (90 - 65 + 1) + 65) produces a random upper case letter.
- If you like, write a function to return a random char each time it is called.
- For example, char c = randomChar() where you write the randomChar function.
- Pass the array to a function called printArray that prints the 2D array with each row on its own line, and each element separated by a space.
- The main function then passes this array to a function called countVowels that does the following:
- Counts the number of vowels generated (a vowel is any of a, e, i, o, u).
- Returns the count of vowels in the 2D array.
- The main function prints the count of vowels in the array.
Example main function that uses these functions:
int main() {
srand(time(0));
char arr[3][3];
fillCharArray(arr, 3, 3);
printArray(arr, 3, 3);
int vowelCount = countVowels(arr, 3, 3);
cout << "The count of vowels in the array is " << vowelCount << endl;
return 0;
}
Sample run of program:
j u n
e k t
q u q
The count of vowels in the array is 3
- Write a complete C++ program that does the following:
- Fills a 2 x 4 char array with random lower case letters.
- Pass the char array to a function called printArray and print it as follows:
- Print each row on its own line
- Print each element separated by a space
- Lists the row indices of rows that contain at least one vowel (a vowel is any of a, e, i, o, u).
- Lists the column indices of columns that contain at least one vowel.
- Outputs the count of vowels and the count of consonants in the 2D array.
Example main function that uses these functions. You do not need to write all the code as functions. You are only required to write the printArray function above. All other code can be written in main if you prefer.
int main() {
srand(time(0));
char arr[2][4];
int rowVowelCount[2] = {0};
int colVowelCount[4] = {0};
fillCharArray(arr, 2, 4);
printArray(arr, 2, 4);
// fill rowVowelCount with count of vowels per row in arr
vowelsPerRow(arr, rowVowelCount, 2, 4);
// fill colVowelCount with count of vowels per row in arr
vowelsPerCol(arr, colVowelCount, 2, 4);
printVowelRows(rowVowelCount, 2);
printVowelCols(colVowelCount, 4);
return 0;
}
Sample run of program:
v j x l
v e n u
Row 1 contains at least one vowel.
Col 1 contains at least one vowel.
Col 3 contains at least one vowel.
Strings
- Write a complete C++ program that does the following:
- Prompt the user for their first name and last name (store in fName and lName variables, respectively)
- Pass the two strings to a function called swapFirstLetters that modifies each string by replacing its first letter with that of the other.
- For example, if fName = Dave and lName = Miller were passed, the strings would be modified by the function to store fName = Mave and lName = Diller.
- Print fName and lName to the monitor.
- Pass the two strings to a function called longestString that prints the larger string and returns the length of the larger string. Store this return value in an int variable called length.
- If both strings are of equal length, you can print whichever string you prefer.
- Pass length to a function called repeatHello that prints "Hello" to the monitor length times.
Example main function that uses these functions:
int main() {
string fName, lName;
int length;
cout << "Please enter your first name: ";
cin >> fName;
cout << "Please enter your last name: ";
cin >> lName;
swapFirstLetters(fName, lName);
cout << "Your new first name is " << fName << endl;
cout << "Your new last name is " << lName << endl;
length = longestString(fName, lName); // this function prints the longest string from inside the function
repeatHello(length);
return 0;
}
Sample run of program:
Please enter your first name: Dave
Please enter your last name: Miller
Your new first name is Mave
Your new last name is Diller
Diller is the longest string.
HelloHelloHelloHelloHelloHello
- Write a complete C++ program that does the following:
- Prompts the user for a string with at least 3 letters in it.
- If the string is too short, force the user to enter a longer string.
- Prompts the user for a non-zero positive integer.
- If the input is invalid, force the user to enter a valid input.
- Pass the string and integer to a function called bookEndString that does the following:
- If the first and last letters of the string are the same, modify the string by adding r copies of that letter to the beginning and end of the string.
- Otherwise, modify the string by adding r Z's to the beginning and end of the string.
- Otherwise, modify the string by adding r Z's to the beginning and end of the string.
- The main function then prints the string.
Example main function that uses bookEndString:
int main() {
string s;
int num;
cout << "Enter a word with at least three letters: ";
cin >> s;
while(FILL IN){
FILL IN
}
cout << "Enter a number greater than 0: ";
cin >> num;
while(FILL IN) {
FILL IN
}
bookEndString(s, num);
cout << "The modified word is: " << s << endl;
return 0;
}
Sample run of program:
Enter a word with at least three letters: hi
Invalid input! Enter a word with at least three letters: hello
Enter a number greater than 0: 0
Invalid input! Enter a number greater than 0: 3
The modified word is: ZZZhelloZZZ
Another sample run of program:
Enter a word with at least three letters: shoes
Enter a number greater than 0: 4
The modified word is: ssssshoesssss
- Write a complete C++ program that does the following:
- Declares two string arrays of capacity 5. Call the first one words and the second one prefixes.
- Prompts the user to enter five strings and store each string in a separate index of the words array.
- Passes the words and prefixes arrays and their capacity to a function called getPrefixes. getPrefixes does the following:
- Copies a substring consisting of the first three letters of each string in words[i] into the corresponding index of prefixes[i]
- If any string in the words array is shorter than three letters, just copy the entire string into the corresponding index of prefixes.
- Passes the prefixes array and its capacity to a function called concatenateStrings that does the following:
- Combines (concatenates) each string in prefixes into one large string, which is returned to main.
- In main, print the returned string.
Example main function that uses these functions:
int main() {
string words[5], prefixes[5];
cout << "Enter five words: ";
readInput(words, 5);
getPrefixes(words, prefixes, 5);
string s = concatenateStrings(prefixes, 5);
cout << "Concatenated string is " << s << endl;
return 0;
}
Sample run of program:
Enter five words: bird cat dog horse ox
Concatenated string is bircatdoghorox